<template>
  <a-layout-footer style="text-align: center">
    信思智学知识库
  </a-layout-footer>
</template>

<script lang="ts">
import { defineComponent,onMounted} from 'vue';
import {Tool} from "@/util/tool";
import { notification } from 'ant-design-vue';
export default defineComponent({
  name: 'the-footer',
  //进行和websocket的连接
  setup() {
    let websocket: any;
    let token: any;
    //创建连接
    const onOpen = () => {
      console.log('WebSocket连接成功，状态码：', websocket.readyState)
    };
    //接收到后台消息
    const onMessage = (event: any) => {
      console.log('WebSocket收到消息：', event.data);
      notification.open({
        message: event.data
      });
    };
    //连接错误
    const onError = () => {
      console.log('WebSocket连接错误，状态码：', websocket.readyState)
    };
    //连接关闭
    const onClose = () => {
      console.log('WebSocket连接关闭，状态码：', websocket.readyState)
    };
    //初始化WebSocket
    const initWebSocket = () => {
      // 连接成功
      websocket.onopen = onOpen;
      // 收到消息的回调
      websocket.onmessage = onMessage;
      // 连接错误
      websocket.onerror = onError;
      // 连接关闭的回调
      websocket.onclose = onClose;
    };
    onMounted(() => {
      // WebSocket
      if ('WebSocket' in window) {
        token = Tool.uuid(10);
        // 连接地址：ws://127.0.0.1:8083/ws/xxx
        websocket = new WebSocket(process.env.VUE_APP_WS_SERVER + '/ws/' + token);
        initWebSocket()
        // 关闭
        // websocket.close();
      } else {
        alert('当前浏览器 不支持')
      }
    });
  }
});
</script>